<?php
namespace app\admin\controller;
use app\common\controller\Sendmsg;
use app\service\billService;
use app\service\roominfoService;
use think\Controller;
use think\facade\Db;
use think\facade\Log;

class Sms extends Common{
    //费用结算
    public function feesettle(){
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);//所有小区ID
        $communitylist = (new roominfoService())->communityList($whe);
        View::assign('communitylist',$communitylist);

        $data = request()->get();
        if (!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            View::assign('where',$where);   
            $where = array_filter($where);
        }else{
            $where = null;
        }
        $year="2019";
        View::assign("year",$year);
        $arr=array("01","02","03","04","05","06","07","08","09","10","11","12");
        foreach ($arr as $key => $value) {
            $month[$value]=Db::name('smslog')->where('community_id','in',$com['com_id'])->where('ctime','like','%-'.$value.'-%')->count();
        }
       
        View::assign("month",$month);
        return View::fetch();
    }

    
    //短信记录
    public function smsloglist(){
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);//所有小区ID
        $communitylist = (new roominfoService())->communityList($whe);
        View::assign('communitylist',$communitylist);

        $community_id = $com['com_id'];
        $data = request()->get();
        if (!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['owner_name'] = $data['owner_name'];//姓名
            }else{
                $where['owner_name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = $data['phone'];//电话
            }else{
                $where['phone'] = "";
            }
           
            View::assign('where',$where);   
            $where = array_filter($where);
        }else{
            $where = null;
        }
        $page = request()->has('page') ? request()->get('page') : 1;
        //查询房屋条数
        $number = Db::name('smslog')
                        ->where('community_id','in',$community_id)
                        ->where($where)
                        ->count();
        View::assign('number',$number);
        //查询房屋信息
        $smslist = Db::name('smslog')
                        ->where('community_id','in',$community_id)
                        ->where($where)
                        ->order('id desc')
                        ->paginate(100, false, [
                        'page'  =>  $page,
                        'query' =>  $where
                    ]);
        View::assign("smslist",$smslist);
        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch("smslog_list");
    }
    public function sendmsg(){
        $id=request()->post("id");
        $billinfo=(new billService())->billIdinfo($id);
        $roominfo=Db::table("roominfo")
                        ->where("out_room_id",$billinfo["out_room_id"])
                        ->find();
    }

    public function smssend(){
        if(request()->isPost()){
            $data=request()->post();
            //var_dump($data);die;
            //查询该房屋24小时之内是否发送信息
            $smsinfo=Db::table("smslog")
                        ->where("billid",$data["billid"])
                        ->where("create_time",'>',time()-24*3600)
                        ->find();
            if(empty($smsinfo)){
                $res=(new Sendmsg())->sendmsg($data["phone"],$data["con"]);
                if($res=="100"){
                    
                    $data["create_time"]=time();
                    $data["operator"]=session("name");
                    Db::table("smslog")->insert($data);
                    return "短信发送成功";
                }else{
                    return "短信发送失败，请重新发送";
                }
            }else{
                return "该条账单24小时内不能重复发送信息";
            }
        }else{
            $id=request()->get("id");
            $billinfo=(new billService())->billIdinfo($id);
            $roominfo=Db::table("roominfo")
                            ->where("out_room_id",$billinfo["out_room_id"])
                            ->find();
            View::assign("roominfo",$roominfo);
            
            
            $billid=$id;
           
            $str="(".$billinfo["cost_type"].")：￥".$billinfo["bill_entry_amount"];
               
           
            $communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
           
            
            $contents="【".$communityinfo["community_name"]."】".$roominfo["owner_name"]."(先生/女士)您住的".$communityinfo["community_name"].$str."未缴纳,请打开支付宝搜索小程序早早番茄缴费";//,https://www.jfzhiwen.com/admin/h5alipay/billinfo?id=".$roominfo["id"]
            View::assign("contents",$contents);
            View::assign("billid",$billid);
            View::assign("communityinfo",$communityinfo);
            return View::fetch();
        }
        

    }
    public function sendmsgcount(){
        $id = explode(',',request()->param('id'));        
        $info = Db::name('bill')->distinct(true)->field("out_room_id")->where('id','in',$id)->select()->toArray();
        $number=count($info);
        return json(['info' => $number]);
       
    }
    public function sendmsgall(){
        $id = explode(',',request()->param('id'));
        $info = Db::name('bill')->where('id','in',$id)->select()->toArray();
        $snum=0;

        foreach ($info as $key => $value) {
            
            $roominfo=Db::table("roominfo")
                        ->where("out_room_id",$value["out_room_id"])
                        ->find();
            //查询该账单24小时之内是否发送信息
            $smsinfo=Db::table("smslog")
                        ->where("billid",$value["id"])
                        ->where("create_time",'>',time()-24*3600)
                        ->find();
            if((empty($smsinfo))&&(!empty($roominfo))){
                //校验手机长度
                $phone=trim($roominfo["owner_phone"]);
                if(strlen($phone)=="11"){

                    $communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
                    $str="(".$value["cost_type"].")：￥".$value["bill_entry_amount"];
                    $billid=$value["id"];
                    
                    
                    $contents="【".$communityinfo["community_name"]."】".$roominfo["owner_name"]."您住的".$communityinfo["community_name"].$str."未缴纳,请打开支付宝-生活缴费-物业费,进行缴纳。感谢您的支持与理解。";
                    $res=(new Sendmsg())->sendmsg($phone,$contents);
                    Log::write($res,"123");
                    if($res=="100"){
                        $snum=$snum+1;
                        $sms["community_id"]=$communityinfo["id"];
                        $sms["community_name"]=$communityinfo["community_name"];
                        $sms["room_id"]=$roominfo["id"];
                        $sms["room_address"]=$roominfo["address"];
                        $sms["phone"]=$phone;
                        $sms["con"]=$contents;
                        $sms["owner_name"]=$roominfo["owner_name"];
                        $sms["create_time"]=time();
                        $sms["operator"]=session("name");
                        $sms["billid"]=$billid;
                        Db::table("smslog")->insert($sms);
                    }
                }
            }else{
               
            }
        }
        return json(['info' => "信息发送完成,成功发送:".$snum."条"]);
    }
}
    